package com.vcar.LDiceClient.chat
{
	import com.vcar.LDiceClient.config.GlobalEventName;
	import com.vcar.LDiceClient.config.MediatorName;
	import com.vcar.LDiceClient.core.ModWindowManager;
	import com.vcar.LDiceClient.dataproxy.ResponseMessage;
	
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class ChatShowMediator extends Mediator
	{

		private var chatPopUp:FChatFrame;
		public function ChatShowMediator(viewComponent:Object=null)
		{
			super(MediatorName.ChatShowMediator, viewComponent);
			chatShow.showChatBtn.addEventListener(MouseEvent.CLICK,showChatInput);
			chatShow.hideChatBtn.addEventListener(MouseEvent.CLICK,hideChatConvas);
		}
		
		public function get chatShow():FChatShow
		{
			return viewComponent as FChatShow;
		}
		
		override public function listNotificationInterests() : Array
		{
			return [GlobalEventName.CHAT_MSG_GET,GlobalEventName.STAGE_HOTKEY_PRESS];
		}
		
		
		
		private function hideChatConvas(event:MouseEvent):void
		{
			chatShow.chatConvas.visible=(chatShow.chatConvas.visible==false)?true:false;
		}
		
		
		/**
		 * @author	龚勇星 
		 * @msg		接收聊天室消息
		 */		
		private function reciveMsg(chatObj:ResponseMessage):void
		{
			var msg:String;
			
			switch( chatObj.subtype){
				case 1:
					//[color=0xFFFFFF][世界]  
					msg="[color=0xFFFFFF]"+chatObj.object.userName+": "+chatObj.object.msg+"\n";
					break;
				case 2:
					//[color=0xf86b39][店铺]  
					msg="[color=0xFFFFFF]"+chatObj.object.userName+": "+chatObj.object.msg+"\n";
					break;
				case 3:
					//[color=0x02a657][同桌]  
					msg="[color=0xFFFFFF]"+chatObj.object.userName+": "+chatObj.object.msg+"\n";				
					break;
				case 4:
					//[color=0x02a657][同桌]  
					msg="[color=0xFFFF00]【公告】"+chatObj.object.msg+"\n";				
					break;
				case 5:
					//[color=0x02a657][同桌]  
					msg="[color=0xFF0000]"+chatObj.object.msg+"\n";				
					break;
			}
			chatShow.TA1.appendRichText(msg);
		}
		override public function handleNotification(notification:INotification) : void
		{
			switch(notification.getName()){
				case GlobalEventName.CHAT_MSG_GET:
					reciveMsg(notification.getBody() as ResponseMessage);
					break;
				case GlobalEventName.STAGE_HOTKEY_PRESS:
					if(facade.retrieveMediator(MediatorName.loginModuleMediator)!=null)
					{
						return;
					}
						
					if(notification.getBody().keyCode==Keyboard.ENTER)
					{
						showChatInput(null);
					}
					break;
			}
		}
		
		
		/**
		 * 点击弹出输入框
		 * @author	何伟章
		 */		
		private function showChatInput(e:MouseEvent):void
		{
			if (chatPopUp != null)
			{
				chatPopUp.visible=(chatPopUp.visible == false) ? true : false;
				if(chatPopUp.visible)
				{
					chatPopUp.tfin.setFocus();
				}
				else
				{
					chatShow.setFocus();
				}
				return;
			}			
			chatPopUp=new FChatFrame();
		
			chatPopUp.x=0;
			chatPopUp.y=0;
			ModWindowManager.getInstance().addComponentPopUp(chatPopUp);
			chatPopUp.tfin.setFocus();
		}
		
	}
}